package com.bwie.sixth.commerce.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
   <dependency>
      <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
      <version>3.8.0</version>
  </dependency>
 */
public class OSSFileUtil {
//用户登录名称 wangmo123@1090330238691707.onaliyun.com
//AccessKey ID LTAI5tLc6rNDMVVckdBw7ZCv
//AccessKey Secret 9EW4S2fIhbzplAVL5QS3i5Zli6tfZ2

    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI5tLc6rNDMVVckdBw7ZCv";
    private static String accessKeySecret = "9EW4S2fIhbzplAVL5QS3i5Zli6tfZ2";
    private static String bucketName = "wangmo123";
    private static String key = "<key>";

    //url上传路径
    public static String uploadFile(MultipartFile file) throws IOException {
        //获取文件名称
        String filename = file.getOriginalFilename();
        //得到一个uuid随机的字符串
        String uuid = UUID.randomUUID().toString();
        //uuid和文件的后缀名称拼接得到一个新的文件名称
        // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        String objectName = uuid+filename.substring(filename.lastIndexOf("."));

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（objectName）

        ossClient.putObject(bucketName, objectName, file.getInputStream());

        // 关闭OSSClient。
        ossClient.shutdown();

        return "https://"+bucketName+"."+endpoint+"/"+objectName;

    }
    //oss下载
    public static String downFile(MultipartFile file, String  url) throws IOException {
        //获取文件路径
        String name = file.getName();
        System.out.println(name);

        return "";
    }



}
